package a0_waittandnotify;

public class foodie extends Thread{
    /* 消费者
    1 写循环->同步代码块->判断->吃->唤醒生产者
    2 加锁
    3 判断
    4 吃
    5 唤醒生产者
     */
    @Override
    public void run() {
        while (true){
            synchronized (desk.lock){
                if (desk.count==0){
                   break;
                }else{
                  if(desk.foodFlag==0){
                      try {
                          desk.lock.wait();//没有饭,等待
                      } catch (InterruptedException e) {
                          throw new RuntimeException(e);
                      }
                  }else{
                      desk.count--;
                      System.out.println("吃货正在吃面,当前剩余"+desk.count+"碗");
                      desk.lock.notifyAll();//吃完唤醒生产者
                      desk.foodFlag=0;
                    }
                }
            }
        }
    }
}
